<%@page import="weaver.general.BaseBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.alibaba.fastjson.JSONArray"%>
<%@page import="weaver.conn.RecordSetDataSource"%>
<%@page import="com.alibaba.fastjson.JSONObject"%>
<%@page import="weaver.general.Util"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String dbSource = Util.null2String(request.getParameter("dbSource")).trim();			//所使用的数据源
	String executeSql = Util.null2String(request.getParameter("executeSql")).trim();		//执行的sql
	int currentPage = Util.getIntValue(request.getParameter("currentPage"),1);				//当前页
	int pageSize = Util.getIntValue(request.getParameter("pageSize"),10);					//每页条数
	String dbType = "sqlservice";
	JSONObject json = new JSONObject();
	boolean isFlag = false;
	String msgText = "执行成功";
	String tabPage = "resultInfo";
	try{
		if(!"".equals(executeSql)){
			RecordSetDataSource rsds;
			if( "default".equals(dbSource) ){ rsds = new RecordSetDataSource(); }else{ rsds = new RecordSetDataSource(executeSql); }
			if( executeSql.startsWith("SELECT") || executeSql.startsWith("select") ){			//查询语句执行
				int totalNum = 0;																//记录总数
				int begin = (currentPage-1) * pageSize;											//开始行数
				if(begin>0) begin = begin+1;
				int end = currentPage * pageSize;												//结束行数
				String columnsSql = "SELECT * FROM ( " + executeSql + " ) A WHERE 1<>1";		//表结构查询sql
				String countSql = "SELECT COUNT(*) TOTALNUM FROM ( " + executeSql + " ) A";		//总记录查询sql
				String pagingSql = "";															//分页查询sql
				if( "oracle".equalsIgnoreCase(dbType) ){
					pagingSql = "SELECT * FROM  (  SELECT A.*, ROWNUM RN FROM (" + executeSql + ") A ) WHERE RN BETWEEN " + begin + " AND " +end;
				}else if( "sqlservice".equalsIgnoreCase(dbType) ){
					pagingSql = "SELECT * FROM ( SELECT A.*,ROW_NUMBER() OVER (ORDER BY ROWNUM) RN FROM ( SELECT A.*,0 ROWNUM FROM ( " + executeSql + " ) A ) A ) A WHERE RN BETWEEN " + begin + " AND " + end;
				}	

				if( rsds.executeSql(columnsSql) ){	//查询结构
					JSONArray columns = new JSONArray();
					List<String> columnsNameList = new ArrayList<String>();
					for(String columnName : rsds.getColumnName()){
						if( !"".equals(Util.null2String(columnName)) ){
							columnsNameList.add(columnName);
							JSONObject columnObj = new JSONObject();
							columnObj.put("key",columnName);
							columnObj.put("title",columnName);
							columnObj.put("width",200);
							columns.add(columnObj);
						}
					}
					if( "default".equals(dbSource) ){ rsds = new RecordSetDataSource(); }else{ rsds = new RecordSetDataSource(executeSql); }
					if( rsds.executeSql(countSql) ){	//查询总记录数
						if(rsds.next())	totalNum = rsds.getInt("TOTALNUM");
						json.put("totalNum",totalNum);
						if( "default".equals(dbSource) ){ rsds = new RecordSetDataSource(); }else{ rsds = new RecordSetDataSource(executeSql); }
						if( rsds.executeSql(pagingSql) ){	//查询数据
							JSONArray columnDatas = new JSONArray();
							while(rsds.next()){
								JSONObject columnData = new JSONObject();
								for(String columnName : columnsNameList){
									columnData.put(columnName,rsds.getString(columnName));
								}
								columnDatas.add(columnData);
							}
							isFlag = true;
							tabPage = "tablePage";
							json.put("datas",columnDatas);
						}else{
							msgText = "查询数据失败!";
						}
					}else{
						msgText = "查询总记录数失败!";
					}
					json.put("columns",columns);
				}else{
					msgText = "执行错误!";
				}
			}else{
				if(rsds.execute(executeSql)){
					isFlag = true;
					msgText = rsds.getMsg();
				}else{
					msgText = "执行错误!";					
				}
			}
		}else{
			msgText = "请输入sql!";			
		}
	}catch(Exception e){
		msgText = e.getMessage();
	}
	json.put("msgText",msgText);
	json.put("isFlag",isFlag);
	json.put("tabPage",tabPage);
	out.print(json.toJSONString());
%>
